package mapengine.building
{
import core.manager.BonusManager;
import core.manager.CostManager;

import eDpLib.events.ProxyEvent;

import isoengine.interfaces.IMapObjectData;

import mapengine.interfaces.IFactoryBuilding;

import utils.TimeUtil;

import values.common.CostValue;
import values.item.ItemValue;
import values.item.building.FactoryBuildingItemValue;

import view.component.menu.FactoryBuildingMenu;
import view.component.progressbar.ActionBar;
import view.component.tip.FactoryBuildingProduceTip;


/**
 * FactoryMapBuilding
 * @author jliang
 *
 */
public class FactoryMapBuilding extends MapBuilding implements IFactoryBuilding
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function FactoryMapBuilding(descriptor:Object=null)
	{
		super(descriptor);
	}
	//==========================================================================
	//  Protected methods
	//==========================================================================		
	private var vo:FactoryBuildingItemValue;
	
	public function get factoryBuildingItemValue():FactoryBuildingItemValue
	{
		return vo;
	}
	
	public function get isProducing():Boolean
	{
		return factoryBuildingItemValue.isProducing;
	}
	
	public function get isProduceComplete():Boolean
	{
		return factoryBuildingItemValue.isProduceComplete;
	}
	
	public function get isWaitingProduce():Boolean
	{
		return factoryBuildingItemValue.isWaitingProduce;
	}
	//==========================================================================
	//  Overridden methods: MapBuilding
	//==========================================================================
	override public function set objectData(value:IMapObjectData):void
	{
		vo = value as FactoryBuildingItemValue;		
		
		super.objectData = value;
	}
	
	override public function get canUpgrade():Boolean
	{
		return super.canUpgrade && isWaitingProduce;
	}
	
	override public function get canStash():Boolean
	{
		return super.canStash;
	}
	
	override public function get canMove():Boolean
	{
		return super.canMove;
	}
	
	override public function get canSell():Boolean
	{
		return super.canSell;
	}
	
	override protected function get actionDefination():Object
	{
		var def:Object = super.actionDefination;
		def["1"] = { state:1, barRef:ActionBar };
		def["3"] = { state:3, barRef:ActionBar };
		return def;
	}
	
	override protected function get menuReference():Class
	{
		return FactoryBuildingMenu;
	}
	
	override public function initialize():void
	{
		super.initialize();
	}
	
	override public function dispose():void
	{
		stopTimer();
		
		super.dispose();
	}
	
	override public function changeState(state:int, params:Object = null):void
	{
		super.changeState(state, params);
		
		stopTimer();
	}
    
    override protected function checkSymbol():void
    {
        //生产状态
        if (isProduceComplete)
        {
            showSymbol("swf.ui.ProduceCompleteSymbol");
        }
        else if (isWaitingProduce)
        {
            showSymbol("swf.ui.WaitingProduceSymbol");
        }
    }
	
	override public function clickFun():void
	{
		if (isDoingAction) return;
		
		if (isWaitingProduce)
		{
			showMenu();
			return;
		}
		
		super.clickFun();
	}
	
	override public function finishBuildAction():void
	{
		factoryBuildingItemValue.resetProduceSetting();
		super.finishBuildAction();
	}
	
	override protected function overHandler(event:ProxyEvent):void 
	{		
		super.overHandler(event);
		
		if (isProducing)
		{
			showTip(FactoryBuildingProduceTip);
		}
	}
	
	override protected function showMenu():void
	{
		if (isProduceComplete) return;
		
		super.showMenu();
	}
	
	override protected function assetReady():void
	{
        super.assetReady();
        
		if (isProducing)
		{
			startTimer();
		}
	}
	//==========================================================================
	//  Public methods
	//==========================================================================	
	public function beforeCollectAction():Boolean
	{
		if (!CostManager.hasEnoughToCost(factoryBuildingItemValue.collectCost))
		{
			return false;
		}
		return true;
	}
	
	public function finishCollectAction():void
	{
        var obj:Object = factoryBuildingItemValue.collectProduct();
        
		factoryBuildingItemValue.resetProduceSetting();
		changeToNextState();
        
        mapModel.collectProduct(factoryBuildingItemValue, 
                                                     ItemValue.convertNumbericItemsToServerObject(CostManager.cost(obj.cost)),
                                                     ItemValue.convertItemsToObjectArray(obj.bonus));
        BonusManager.addItemBonus(obj.bonus);
	}
	
	public function produce(productKey:String, time:Number = 0):void
	{
		var cost:CostValue = factoryBuildingItemValue.startProduce(productKey, time);	
        if (cost)
        {
            mapModel.startProduce(factoryBuildingItemValue, 
                                                      factoryBuildingItemValue.currentState.nextState,
                                                      ItemValue.convertNumbericItemsToServerObject(CostManager.cost(cost)));
        }
		doAction();
	}
	
	public function startProduceAction():void
	{
		changeToNextState();
		startTimer();
	}
	//==========================================================================
	//  Protected methods
	//==========================================================================	
	protected function startTimer():void
	{
		TimeUtil.addRenderFunction(produceProgressHandler);
	}
	
	protected function stopTimer():void
	{
		TimeUtil.removeRenderFunction(produceProgressHandler);
	}
	//==========================================================================
	//  Event handlers
	//==========================================================================
	protected function produceProgressHandler():void
	{
		var rest:Number = factoryBuildingItemValue.produceTimeRest;
		trace("生产时间剩余:   " + rest + " 秒");
		if (rest <= 0)
		{
			trace("生产完成.");
			changeToNextState();
		}
	}
}
}




